"use strict"

// const devUrl = 'http://192.168.3.41:8684'
// const devUrl = 'http://kujiang.ailole.cn'
const devUrl = 'https://admin.aoxiang.com';

const baseRequest = (url, params, methods) => {
	let baseDefaultOpts = {
		url: devUrl + url,
		data: params,
		method: methods && methods.toUpperCase(),
		header: {
			"Content-Type": "application/json; charset=UTF-8"
		}
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(baseDefaultOpts).then(
			(res) => {
				if (res[1].data.code == 200) {
					uni.hideLoading();
					resolve(res[1].data)
				} else {
					uni.hideLoading();
					uni.showToast({
						title: res[1].data.msg,
						icon: 'none'
					})
				}
			}
		).catch(
			(error) => {
				reject(error)
			}
		)
	})
	return promise
};

const tokenRequest = (url, params, methods) => {
	let token = uni.getStorageSync("tk");
	let DefaultOpts = {
		url: devUrl + url,
		data: params,
		method: methods && methods.toUpperCase(),
		header: methods == 'POST' ? {
			'token': token,
			"Content-Type": "application/json; charset=UTF-8"
		} : {
			'token': token,
			'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
		}
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(DefaultOpts).then(
			(res) => {
				if (res[1].data.code == 200) {
					uni.hideLoading();
					resolve(res[1].data)
				} else {
					uni.hideLoading();
					uni.showToast({
						title: res[1].data.msg,
						icon: 'none'
					})
				}
			}
		).catch(
			(error) => {
				reject(error)
			}
		)
	})
	return promise
}

export {
	devUrl,
	baseRequest,
	tokenRequest
}
